package com.zlt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zlt.annotation.Transactional;
import com.zlt.dto.VipLogDTO;
import com.zlt.entity.ChargeRecord;
import com.zlt.entity.Vip;
import com.zlt.entity.VipLog;
import com.zlt.invocation.TansactionInvocation;
import com.zlt.service.VipService;
import com.zlt.service.impl.VipServiceImpl;
import com.zlt.util.*;
import com.zlt.util.ProxyCreator;
import com.zlt.vo.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PushbackInputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/vip/*")
public class VIPController extends BaseController {

    VipService vipService = new VipServiceImpl();


    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }



    public Result list(HttpServletRequest req, HttpServletResponse resp){

        List<VipVO> vipVOS = vipService.selectVipAllType();


        return Result.ok("vip类型查询成功",vipVOS);
    }
    ////先支付
    public Result pay(HttpServletRequest req, HttpServletResponse resp){

        //支付之前先检查用户是否登录

        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser==null){
            return Result.error("用户未登录");
        }

        //用户登录后 还要检查是否为招聘者
//        "f0f24449-9082-4a94-ae45-7c8f6710dd09"

//////////////////////////////////////////////////////////////////////////////
        VipLogDTO vipLogDTO = ClassUtil.resolveRequest(VipLogDTO.class, req);
        vipLogDTO.setUid(activeUser.getUid());

        //查询充值的vip类型

        Vip vip =  vipService.selectVipType(vipLogDTO.getVid());
        System.out.println("vip:"+vip);

        vipLogDTO.setMoney(vip.getPrice());


        //生成订单号
        String orderNo = GenerateNum.getInstance().GenerateOrder();
        vipLogDTO.setOrderNo(orderNo);


//        ServletContext servletContext = req.getServletContext();
//        servletContext.setAttribute("vipLogDTO",vipLogDTO);

        int i = vipService.insertChargeRecord(vipLogDTO);
        System.out.println("vipLogDTO==>>>>>>"+vipLogDTO);


        String form = AlipayUtil.pay(vipLogDTO.getOrderNo(), vipLogDTO.getMoney().toString(), "会员充值");

//         boolean bool =  vipService.charge(vipLogDTO);
        System.out.println("vipLogDTO:"+vipLogDTO);

        return Result.ok("会员充值中",form);
    }


    //支付成功后 在进行数据库数据处理

    public Result promote(HttpServletRequest req, HttpServletResponse resp){

        String out_trade_no = req.getParameter("out_trade_no");

        System.out.println("out_trade_no=======>>>>>>>>"+out_trade_no);


        ChargeRecord chargeRecord = vipService.selectChargeRecord(out_trade_no);
        System.out.println("chargeRecord======>>>>>"+chargeRecord);



        VipLogDTO vipLogDTO = new VipLogDTO();
        vipLogDTO.setUid(chargeRecord.getUid());
        vipLogDTO.setMoney(chargeRecord.getMoney());
        vipLogDTO.setOrderNo(chargeRecord.getOrderNo());
        vipLogDTO.setVid(chargeRecord.getVid());

        System.out.println("vipLogDTO=======?>>>>>>>>>>"+vipLogDTO);





        //获取当前用户的信息
//        ServletContext servletContext = req.getServletContext();


        System.out.println("vipLogDTO111:"+vipLogDTO);
        String orderNo = vipLogDTO.getOrderNo();

        String query = AlipayUtil.query(orderNo);

        JSONObject jsonObject = JSON.parseObject(query,JSONObject.class);
        System.out.println("jsonObject:"+jsonObject);

        JSONObject alipay_trade_query_response = (JSONObject) jsonObject.get("alipay_trade_query_response");
        String trade_status = alipay_trade_query_response.getString("trade_status");
        System.out.println("trade_status::::"+trade_status);

        //说明支付成功了
        if (trade_status.equals("TRADE_SUCCESS")){
            //用户充值成功 插入充值记录
            vipLogDTO.setCreateTime(TimeUtil.getCurrentDateString());

            int i = vipService.insertChargeLog(vipLogDTO);
            //延长vip到期时间 或 充值为vip
            boolean bool =  vipService.charge(vipLogDTO);

            return bool?Result.ok("会员充值成功!"):Result.ok("会员充值失败!");

        }

        //充值成功后  更改用户数据库中的vip信息
//

        return Result.error("会员充值失败!",vipLogDTO);
    }





    //实时获取vip的状态

    public Result vipState(HttpServletRequest req, HttpServletResponse resp){

        ActiveUser activeUser = ActiveUserUtil.get();

//        String uid = "f0f24449-9082-4a94-ae45-7c8f6710dd09";
        String uid = activeUser.getUid();

        UserVipVO userVipVO =   vipService.selectVipState(uid);

        return Result.ok("查询用户当前的vip状态",userVipVO);

    }


    //


    public Result vipJob(HttpServletRequest req, HttpServletResponse resp){

        //对于岗位  我们不需要验证招聘者是否登录  只需要查询vip企业的岗位信息 进行返回
        //不需要参数
        List<JobVo> jobVos = vipService.selectVipJobs();

        return Result.ok("vip推荐岗位",jobVos);

    }



    public Result recentJob(HttpServletRequest req, HttpServletResponse resp){

        List<JobVo> jobVos = vipService.selectRecentJobs(9);

        return Result.ok("最新岗位",jobVos);


    }


    //查询充值记录

    public Result chargeRecord(HttpServletRequest req, HttpServletResponse resp){

//        String uid = "f0f24449-9082-4a94-ae45-7c8f6710dd09";
        ActiveUser activeUser = ActiveUserUtil.get();
        String uid = activeUser.getUid();

        List<VipLogVO> vipLogs = vipService.selectVipLogs(uid);


        return Result.ok("充值记录",vipLogs);
    }







}
